package com.LC._169;

import java.util.HashMap;
import java.util.Map;

class Solution {
    public int majorityElement(int[] nums) {
        Map<Integer, Integer> counts = countNums(nums);
        Map.Entry<Integer, Integer> majorityEntry = null;
        for(Map.Entry<Integer, Integer> entry : counts.entrySet()) {
            if(majorityEntry==null|| entry.getValue()>majorityEntry.getValue()){
                majorityEntry = entry;
            }
        }
        return majorityEntry.getKey();
    }
    private Map<Integer, Integer> countNums(int[] nums) {
        Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
        for(int i=0;i<nums.length;i++){
            if(counts.containsKey(nums[i])==false){
                counts.put(nums[i] , 1);
            }else{
                counts.put(nums[i] , counts.get(nums[i])+1);
            }
        }
        return counts;
    }
    public static void main(String[] args) {
        Solution sol = new Solution();
        int[] nums= new int[]{3,2,3};
        System.out.println(sol.majorityElement(nums));
    }
}